package com.JobWebGem.system.service.impl;

import com.JobWebGem.system.mapper.ResumeMapper;
import com.JobWebGem.system.model.dto.resume.ResumeQueryRequest;
import com.JobWebGem.system.model.entity.Resume;
import com.JobWebGem.system.model.vo.ResumeVO;
import com.JobWebGem.system.service.ResumeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author renli
 * @description 针对表【resume(存储简历信息)】的数据库操作Service实现
 * @createDate 2024-12-02 21:18:40
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume>
        implements ResumeService {

    @Resource
    private ResumeMapper resumeMapper;

    @Override
    public Page<ResumeVO> getByMy(Page<Resume> paramPage, ResumeQueryRequest resumeQueryRequest, Integer userId) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.orderByDesc("createTime");
        // 查询 Resume 实体
        Page<Resume> page = this.page(paramPage, queryWrapper);

        // 将 Resume 实体转换为 ResumeVO
        Page<ResumeVO> resumeVOPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<ResumeVO> resumeVOList = page.getRecords().stream()
                .map(resume -> {
                    ResumeVO resumeVO = new ResumeVO();
                    // 先复制所有基础属性
                    BeanUtils.copyProperties(resume, resumeVO);
                    // 设置resumeId
                    resumeVO.setResumeId(resume.getId());
                    // 设置性别描述
                    if (resume.getSex() == 1) {
                        resumeVO.setSex("女");
                    } else if (resume.getSex() == 0) {
                        resumeVO.setSex("男");
                    }
                    // 设置头像
                    resumeVO.setUserAvatar(resume.getPhoto());
                    return resumeVO;
                })
                .collect(Collectors.toList());

        resumeVOPage.setRecords(resumeVOList);

        return resumeVOPage;
    }

    @Override
    public Page<ResumeVO> getByAll(Page<Resume> paramPage, ResumeQueryRequest resumeQueryRequest) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createTime");
        // 查询 Resume 实体
        Page<Resume> page = this.page(paramPage, queryWrapper);

        // 将 Resume 实体转换为 ResumeVO
        Page<ResumeVO> resumeVOPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<ResumeVO> resumeVOList = page.getRecords().stream()
                .map(resume -> {
                    ResumeVO resumeVO = new ResumeVO();
                    // 先复制所有基础属性
                    BeanUtils.copyProperties(resume, resumeVO);
                    // 设置resumeId
                    resumeVO.setResumeId(resume.getId());
                    // 设置性别描述
                    if (resume.getSex() == 1) {
                        resumeVO.setSex("女");
                    } else if (resume.getSex() == 0) {
                        resumeVO.setSex("男");
                    }
                    // 设置头像
                    resumeVO.setUserAvatar(resume.getPhoto());
                    return resumeVO;
                })
                .collect(Collectors.toList());

        resumeVOPage.setRecords(resumeVOList);

        return resumeVOPage;
    }

}
